<template>
  <div class="exam-detail">
    <exam
      ref="exam"
      :check1="check1"
      :check2="check2"
      @handanswer="handanswer"
    />

    <fixed-bottom>
      <van-button block type="primary" @click="submit()">提交</van-button>
    </fixed-bottom>
  </div>
</template>

<script>
import FixedBottom from "@/components/FixedBottom";
import { Button, Icon } from "vant";
import { saveExam } from "./api";
import exam from "./components/exam";
import { mapGetters } from "vuex";
import Taro from "@tarojs/taro";
export default {
  components: {
    FixedBottom,
    exam,
    [Button.name]: Button,
    [Icon.name]: Icon
  },
  data() {
    return {
      questions: [],
      answer: "",
      check1: "",
      check2: "",
      form: {}
    };
  },
  computed: {
    ...mapGetters(["exam"]),
    question() {
      return this.questions[this.index - 1] || {};
    }
  },
  onLoad(options) {
    this.getDetail(options.type);
  },
  methods: {
    getDetail(type) {
      const types = type.split(",");
      const [check1, check2] = types;
      this.check1 = check1;
      this.check2 = check2;
    },
    async submit() {
      await this.$refs.exam.setAnswer();
      this.save();
    },
    handanswer(e) {
      this.form = { ...e };
    },
    async save() {
      const data = await saveExam(this.form);
      this.showMsg("已保存", "success");
      Taro.redirectTo({
        url: `/pages/exam/detail?id=${data.id}`
      });
    }
  }
};
</script>

<style lang="less">
@import "./index.less";
</style>
